<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%
String usernameSC = (String) session.getAttribute("session_name");
String idSC = (String) session.getAttribute("id");
String typeSC = (String) session.getAttribute("type");
if(usernameSC == null || !(typeSC.equals("committee")))
	response.sendRedirect("error.jsp");
	
	
String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/projects";
String username = "ia";
String password = "123";
String [] proj_id = null;
String [] proj_title = null;
String [][] proj_st_id = null;
String [] st_id = null;
String [] st_name = null;
int size = 0;
int size1 = 0;
int size2 = 0;
try
{
	Class.forName(driver).newInstance();
	
	Connection connection = DriverManager.getConnection(url, username, password);
	
	Statement statement = connection.createStatement();
	Statement statement1 = connection.createStatement();
	String query = "select count(*) from projects where status='accepted'";
	String query1 = "select * from projects where status='accepted'";
	ResultSet rs = statement.executeQuery(query);
	if(rs.next())
	{
		size = rs.getInt(1);
	}
	proj_id = new String[size];
	proj_title = new String[size];
	ResultSet rs1 = statement1.executeQuery(query1);
	for(int i=0 ;rs1.next();i++)
	{
		proj_id[i] = rs1.getString("id");
		proj_title[i] = rs1.getString("title");
	}
	
	String query2 = "select count(*) from student st, user_log_in u where u.ref_id=st.id and u.status='accepted' and u.type='student' and st.proj_id=0";
	String query3 = "select * from student st, user_log_in u where u.ref_id=st.id and u.status='accepted' and u.type='student' and st.proj_id=0";
	ResultSet rs2 = connection.createStatement().executeQuery(query2);
	if(rs2.next())
	{
		size1 = rs2.getInt(1);
		st_id = new String[size1];
		st_name = new String[size1];
	}
	ResultSet rs3 = connection.createStatement().executeQuery(query3);
	for(int i=0;rs3.next();i++)
	{
		st_id[i] = rs3.getString("id");
		st_name[i] = rs3.getString("first_name") + " " + rs3.getString("last_name");
	}
	
	String query4 = "select count(*) from join_in";
	String query5 = "select * from join_in";
	ResultSet rs4 = connection.createStatement().executeQuery(query4);
	if(rs4.next())
	{
		size2 = rs4.getInt(1);
		proj_st_id = new String[size2][2];
	}
	
	ResultSet rs5 = connection.createStatement().executeQuery(query5);
	for(int i=0;rs5.next() && i<size2;i++)
	{
		proj_st_id[i][0] = rs5.getString("proj_id");
		proj_st_id[i][1] = rs5.getString("st_id");
	}
	
	connection.close();
		
}catch(ClassNotFoundException cnfe)
{
		out.println("<br>Couldn't find class file" + cnfe); 
}catch(SQLException sqle)
{
		out.println("<br>SQL Exception: " + sqle);
}catch(InstantiationException ie)
{
		out.println("<br>Instant exception : " + ie);
}catch(IllegalAccessException iae)
{
		out.println("<br>Illegal access exception : " + iae);
}

%>
<script language="javascript">
var selections = new Array();
<%
for(int i=0;i<proj_id.length;i++)
{
	out.println("selections["+i+"] = "+proj_id[i] + ";");
}
%>
</script>
<form name="assignForm" method="GET" action="assignS.jsp" onsubmit="return selectAll()">
<table style="text-align: left" border="0" width="100%" cellspacing="0" cellpadding="0" class="all">
<%
for(int i=0; i < proj_id.length;i++)
{
%>
<tr>
	<td>
		<%=proj_title[i]%>
	</td>
	<td>
		<select id="pr<%=proj_id[i]%>" name="pr<%=proj_id[i]%>" style="width: 100px" multiple>
		<%
		for(int j=0;j<size2;j++)
		{
			if(proj_st_id[j][0].equals(proj_id[i]))
			{
				for(int k=0;k<st_id.length;k++)
				{
					if(st_id[k].equals(proj_st_id[j][1]))
						out.println("<option value='"+st_id[k]+"'>"+st_name[k]);
				}
			}
		}
		%>
		</select>
	</td>
	<td>
		<table>
		<tr><td><input type="button" value="<" onclick="moveSelection(<%=proj_id[i]+","+i%>)"></td></tr>
		<tr><td><input type="button" value=">" onclick="moveSelection1(<%=proj_id[i]+","+i%>)"></td></tr>
		</table>
	</td>
	<td>
		<select style="width: 100px" id="pro<%=i%>" name="pro<%=i%>">
		<%
		ArrayList a = new ArrayList();
		ArrayList b = new ArrayList();
		for(int j=0;j<st_id.length;j++)
		{
			boolean check=false;
			for(int k=0;k<size2;k++)
			{
				if(st_id[j].equals(proj_st_id[k][1]) && proj_id[i].equals(proj_st_id[k][0]))
				{
					check = true;
				}
			}
			if(!check)
			{
				a.add(st_id[j]);
				b.add(st_name[j]);
			}
			
		}
		for(int c=0;c<a.size();c++)
			out.println("<option value='"+a.get(c)+"'>"+b.get(c));
		%>
		</select>
	</td>
</tr>
<%
}
%>
</table>
<center><input type="submit" value="&nbsp;&nbsp;&nbsp;&nbsp;save&nbsp;&nbsp;&nbsp;&nbsp;"/></center>
</form>